import { _decorator, Component, Node, isValid, find } from 'cc';
import { GameSceneUI, GameState } from './GameSceneUI';
const { ccclass, property } = _decorator;

@ccclass('Award')
export class Award extends Component {
    //奖励类型
    @property
    awardType:number =0;

    canvasY:number = 0;

    isContac:boolean = false;//是否已经触发碰撞

    start() {
        const canvasNode = find('Canvas');
        this.canvasY = -canvasNode.position.y;
    }

    private destory()
    {
        this.scheduleOnce(() => {
            if(isValid(this.node))
            {
                this.node.destroy();
            }
        });
    }

    update(deltaTime: number) {
        if(GameSceneUI.getInstance().getGameState() == GameState.Over)
        {
            this.enabled = false;
            this.destory();
            return;
        }

        if(this.node.position.y < this.canvasY)
        {
            this.enabled = false;
            this.destory();
        }

        const position = this.node.position;
        this.node.setPosition(position.x,position.y-deltaTime*150,position.z);
    }

    public getAwardType():number
    {
        return this.awardType;
    }

    public Contac()
    {
        this.isContac = true;
    }

    public getContac():boolean
    {
        return this.isContac;
    }
}


